package com.jiezheng.huinong.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.jiezheng.huinong.biz.ImportFile;
import com.jiezheng.huinong.model.ImportInfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UploadFileAction extends ActionSupport {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	private String bizType;

	// 封装上传文件域的属性
	private File index;

	// 封装上传文件类型的属性
	private String indexContentType;

	// 封装上传文件名的属性
	private String indexFileName;

	// 接受依赖注入的属性
	private String savePath;
	
	// 导入数据统计结果
	
	private ImportInfo imp = new ImportInfo();

	@Override
	public String execute() {
		FileOutputStream fos = null;
		FileInputStream fis = null;
		String filePath = null;
		try {
			filePath = getSavePath() + "\\" + getIndexFileName();
			// 建立文件输出流
			System.out.println(filePath);
			fos = new FileOutputStream(filePath);

			// 建立文件上传流
			fis = new FileInputStream(getIndex());
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}

		} catch (Exception e) {
			System.out.println("文件上传失败");
			e.printStackTrace();
		} finally {
			close(fos, fis);
		}

		// 处理文件
		try {
			imp = processFile(filePath);

		} catch (Exception e) {
			System.out.println("文件处理失败");
			e.printStackTrace();
			imp.setErrorProcess(true);
		}
		
		return SUCCESS;
	}

	/**
	 * 上传后处理
	 */
	public ImportInfo processFile(String path) {
		if (bizType.equals("people")) {
			Map<String, Object> session = ActionContext.getContext().getSession();
			String deptNo = (String)session.get("deptNo");
			return ImportFile.importPeopleFile(path, deptNo);
		} else if (bizType.equals("apply")) {
			Map<String, Object> session = ActionContext.getContext().getSession();
			String deptNo = (String)session.get("deptNo");
			return ImportFile.importApplyFile(path, deptNo);
		} else if (bizType.equals("index")) {
			Map<String, Object> session = ActionContext.getContext().getSession();
			String User = (String)session.get("user");
			String deptNo = (String)session.get("deptNo");
			return ImportFile.importIndexFile(path, User, deptNo);
		}
		else{
			return null;
		}
	}

	/**
	 * 返回上传文件的保存位置
	 * 
	 * @return
	 */
	public String getSavePath() throws Exception {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	private void close(FileOutputStream fos, FileInputStream fis) {
		if (fis != null) {
			try {
				fis.close();
			} catch (IOException e) {
				System.out.println("FileInputStream关闭失败");
				e.printStackTrace();
			}
		}
		if (fos != null) {
			try {
				fos.close();
			} catch (IOException e) {
				System.out.println("FileOutputStream关闭失败");
				e.printStackTrace();
			}
		}
	}

	public File getIndex() {
		return index;
	}

	public void setIndex(File index) {
		this.index = index;
	}

	public String getIndexContentType() {
		return indexContentType;
	}

	public void setIndexContentType(String indexContentType) {
		this.indexContentType = indexContentType;
	}

	public String getIndexFileName() {
		return indexFileName;
	}

	public void setIndexFileName(String indexFileName) {
		this.indexFileName = indexFileName;
	}

	public String getBizType() {
		return bizType;
	}

	public void setBizType(String bizType) {
		this.bizType = bizType;
	}
	public ImportInfo getImp() {
		return imp;
	}

	public void setImp(ImportInfo imp) {
		this.imp = imp;
	}
}